home *** CD-ROM | disk | FTP | other *** search
/ BBS Toolkit / BBS Toolkit.iso / rbbs_pc / newz20.zip / NEWZ.C next >
C/C++ Source or Header  |  1992-06-16  |  12KB  |  429 lines

  1.  
  2. /******************************************************
  3.  NEWZ news files generator for RBBS-PC ver 2.0
  4.  Creates plain and ansi color text files from user
  5.  generated text files with stop/start dates.
  6.  
  7.  Compiled under TURBO C++ 3.0 Small memory model.
  8.  Copyright (c) 1992 by James P. Storch
  9.  all right reserved
  10.  
  11.  Electric Harbor BBS (703) 730-0542
  12.  ******************************************************/
  13.  
  14. #include <dos.h>
  15. #include <stdio.h>
  16. #include <dir.h>
  17. #include <string.h>
  18. #define FALSE 0
  19. #define TRUE !FALSE
  20. #define MAXLINE 160
  21. int main(void);
  22. void no_news(void);
  23.  
  24. FILE *infile;
  25. FILE *outfile;
  26. FILE *outfilec;
  27. FILE *outfileg;
  28.  
  29. struct nwzf {              /* structure for holding nwz file info */
  30.     char name[81];
  31.     long start;
  32.     long stop;
  33.     };
  34.  
  35. struct nwzf nwz[50],temp;
  36. struct date date;     /* date structure, from DOS.H                  */
  37. struct ffblk ffblk;   /* dos file control block structure,from DOS.H */
  38.  
  39. long today;
  40. char line_in[161];
  41. char line_out[161];
  42. char line_outc[161];
  43. char line_outg[161];
  44. int done,count,hi,lo;
  45. int odd,z,x;
  46. long year,month,day;
  47.  
  48. /**********************
  49.  Box drawing characters
  50.  **********************/
  51.  
  52. char tl[]= " ┌─┬";
  53. char tla[]=" (@)";
  54.  
  55. char tr[]= "┬─┐\n";
  56. char tra[]="(@)\n";
  57.  
  58. char le[]= " │o│";
  59. char lea[]="  | ";
  60.  
  61. char re[]= "│o│\n";
  62. char rea[]=" | \n";
  63.  
  64. char bl[]= " └─┴";
  65. char bla[]=" (@)";
  66.  
  67. char br[]= "┴─┘\n";
  68. char bra[]="(@)\n";
  69.  
  70. char hb[]= "                                                                 ";
  71.  
  72. char hl[]= "─────────────────────────────────────────────────────────────────────────";
  73. char hla[]="-------------------------------------------------------------------------";
  74.  
  75.  
  76. char main_path[80];      /* holds path for plain text file to create */
  77. char mainc_path[80];     /*                ansi color file to create */
  78. char maing_path[80];     /*                IBM char file to create   */
  79. char nwz_search[80];     /*                search path and wildcard  */
  80. char nwz_path[80];       /*                search path only          */
  81. char ltk[80];
  82.  
  83. char esc[]="\x1b[";       /* start of ansi control string       */
  84. char border[30];          /* holds ansi string for border color */
  85. char title[30];           /*                       title color  */
  86. char text[30];            /*                       text color   */
  87. char normal[30];          /* ansi string to normalize at end    */
  88.  
  89. int main(void){
  90.  
  91. /************************************************
  92.  Find out what today is and convert to a long int
  93.  ************************************************/
  94.  
  95. getdate(&date);
  96. today=date.da_day+(31*(long)date.da_mon)+(372*(long)date.da_year);
  97.  
  98. /************************************
  99.  Open config file and get config info
  100.  ************************************/
  101.  
  102. printf("--> Reading NEWZ.CFG\n");
  103. if ((infile=fopen("newz.cfg","rt"))==NULL){
  104.     printf("FAILURE TRYING TO OPEN NEWZ.CFG!\n");
  105.     exit(1);
  106.     }
  107. /*******************************************
  108.  Read the paths for files to read and create
  109.  *******************************************/
  110. fscanf(infile,"%s",main_path);
  111. fscanf(infile,"%s",mainc_path);
  112. fscanf(infile,"%s",maing_path);
  113. fscanf(infile,"%s",line_in);
  114. strcpy(nwz_search,line_in);
  115. strcpy(nwz_path,strtok(line_in,"*"));
  116.  
  117. /***************************
  118.  Get License to Kill setting
  119.  ***************************/
  120. fscanf(infile,"%s",ltk);
  121.  
  122. /*******************************************
  123.  Convert color codes to ansi control strings
  124.  *******************************************/
  125. fscanf(infile,"%s",line_in);
  126. strcpy(border,esc);
  127. strcat(border,line_in);
  128. strcat(border,"m");
  129.  
  130. fscanf(infile,"%s",line_in);
  131. strcpy(title,esc);
  132. strcat(title,line_in);
  133. strcat(title,"m");
  134.  
  135. fscanf(infile,"%s",line_in);
  136. strcpy(text,esc);
  137. strcat(text,line_in);
  138. strcat(text,"m");
  139.  
  140. fscanf(infile,"%s",line_in);
  141. strcpy(normal,esc);
  142. strcat(normal,line_in);
  143. strcat(normal,"m");
  144.  
  145. fclose(infile);
  146.  
  147. /*************************************
  148.  Find all .NWZ files in this directory
  149.  *************************************/
  150.  
  151. printf("--> Looking for files matching '%s'\n",nwz_search);
  152.  
  153. if (findfirst(nwz_search,&ffblk,0)) no_news();
  154.  
  155. do{
  156.     strcpy(nwz[count].name,nwz_path);
  157.     strcat(nwz[count].name,ffblk.ff_name);
  158.     printf("    %s found.\n", nwz[count].name);
  159.     count++;
  160.     if (count==50) {
  161.         printf("    Stopping at 50th file!\n");
  162.         break;
  163.         }
  164.     done = findnext(&ffblk);
  165.     }while(!done);
  166.  
  167. /******************************************************
  168.  Now, open each one and get stop & start info from each
  169.  ******************************************************/
  170.  
  171. printf("--> Getting start and stop dates for %d files.\n",count);
  172.  
  173. for(x=0;x<count;x++){
  174.     printf("    Reading stop/start date from %s\n",nwz[x].name);
  175.     if ((infile=fopen(nwz[x].name,"rt"))==NULL){
  176.         printf("FAILURE TRYING TO OPEN %s!\n",nwz[x].name);
  177.         exit(1);
  178.         }
  179.     fscanf(infile,"%s",line_in);
  180.     month= atoi(strtok(line_in,"/"));
  181.     day = atoi(strtok(NULL,"/"));
  182.     year= atoi(strtok(NULL,"\n"));
  183.     if(year<1900) year+=1900;       //Now you can enter 1992 or just 92
  184.     nwz[x].start=day+(31*month)+(372*year);
  185.  
  186.     fscanf(infile,"%s",line_in);
  187.     month= atoi(strtok(line_in,"/"));
  188.     day = atoi(strtok(NULL,"/"));
  189.     year= atoi(strtok(NULL,"\n"));
  190.     if(year<1900) year+=1900;
  191.     nwz[x].stop=day+(31*month)+(372*year);
  192.  
  193.     if(nwz[x].start<=today && nwz[x].stop >=today) z=TRUE;
  194.     fclose(infile);
  195. }
  196.  
  197. /********************************************
  198.  Perform a quick sort of files by start date,
  199.  so the most recent NWZ text are shown first
  200.  ********************************************/
  201. printf("--> Sorting by most recent start dates.\n");
  202. for(hi=0;hi<count-1;hi++)
  203.     for(lo=hi+1;lo<count;lo++)
  204.         if (nwz[hi].start<nwz[lo].start)
  205.         {    temp=nwz[hi];
  206.             nwz[hi]=nwz[lo];
  207.             nwz[lo]=temp;
  208.         }
  209.  
  210. /***************************************
  211.  License to Kill old files, if ltk="yes"
  212.  ***************************************/
  213. if(strnicmp(ltk,"yes",3)==0){
  214.     printf("--> Licensed To Kill! Looking for OLD files.\n");
  215.     for(x=0;x<count;x++){
  216.         if(nwz[x].stop<today){
  217.             printf("    Deleting %s\n",nwz[x].name);
  218.             remove(nwz[x].name);
  219.             }
  220.         }
  221.     } else printf("--> Not licensed to Kill, OLD files left alone.\n");
  222.  
  223. /************************************
  224.  If no files are current do "no news"
  225.  ************************************/
  226. if (z==FALSE) no_news();
  227.  
  228. /********************************
  229.  Open Main.nws for plain text and
  230.  Mainc.nws for ansi colored text
  231.  ********************************/
  232.  
  233. printf("--> Generating files:\n");
  234. printf("    %s\n",main_path);
  235. printf("    %s\n",mainc_path);
  236. printf("    %s\n",maing_path);
  237.  
  238. if((outfile=fopen(main_path,"wt"))==NULL){
  239.     printf("FAILURE TRYING TO OPEN MAIN.NWS!\n");
  240.     exit(1);
  241.     }
  242.  
  243. if((outfilec=fopen(mainc_path,"wt"))==NULL){
  244.     printf("FAILURE TRYING TO OPEN MAINC.NWS!\n");
  245.     exit(1);
  246.     }
  247.  
  248. if((outfileg=fopen(maing_path,"wt"))==NULL){
  249.     printf("FAILURE TRYING TO OPEN MAING.NWS!\n");
  250.     exit(1);
  251.     }
  252.  
  253. /***************************************************************
  254.  Now, open the sorted files that have start dates prior or equal
  255.  to today and stop dates after or equal to today
  256.  ***************************************************************/
  257. for(x=0;x<count;x++){
  258. if(nwz[x].start <= today && nwz[x].stop >= today){
  259.     printf("    Reading news data from %s\n",nwz[x].name);
  260.     if ((infile=fopen(nwz[x].name,"rt"))==NULL){
  261.         printf("FAILURE TRYING TO RE-OPEN %s!\n",nwz[x].name);
  262.         exit(1);
  263.         }
  264.     /********************************************
  265.      Two dummy reads to skip start and stop dates
  266.      ********************************************/
  267.     fgets(line_in,MAXLINE,infile);
  268.     fgets(line_in,MAXLINE,infile);
  269.  
  270.     /************************************
  271.      Get title and format with top border
  272.      ************************************/
  273.     fgets(line_in,MAXLINE,infile);
  274.     strcpy(line_in,strtok(line_in,"\n"));
  275.     /******************************
  276.      odd=1 if length of line is odd
  277.      ******************************/
  278.     z=strlen(line_in);
  279.     odd=z-((z/2)*2);
  280.  
  281.     /*******************
  282.      make plain top line
  283.      *******************/
  284.     strcpy(line_out,tla);
  285.     strncat(line_out,hla,(72-strlen(line_in)-2)/2);
  286.     strcat(line_out,"]");
  287.     strncat(line_out,line_in,strlen(line_in));
  288.     strcat(line_out,"[");
  289.     strncat(line_out,hla,(72-strlen(line_in)-2)/2+odd);
  290.     strcat(line_out,tra);
  291.     fputs(line_out,outfile);
  292.  
  293.     /**********************
  294.      make IBM char top line
  295.      **********************/
  296.     strcpy(line_outg,tl);
  297.     strncat(line_outg,hl,(72-strlen(line_in)-2)/2);
  298.     strcat(line_outg,"┤");
  299.     strncat(line_outg,line_in,strlen(line_in));
  300.     strcat(line_outg,"├");
  301.     strncat(line_outg,hl,(72-strlen(line_in)-2)/2+odd);
  302.     strcat(line_outg,tr);
  303.     fputs(line_outg,outfileg);
  304.     printf("%s",line_outg);
  305.  
  306.     /************************
  307.      make ansi color top line
  308.      ************************/
  309.     strcpy(line_outc,border);
  310.     strcat(line_outc,tl);
  311.     strncat(line_outc,hl,(72-strlen(line_in)-2)/2);
  312.     strcat(line_outc,"┤");
  313.     strcat(line_outc,title);
  314.     strncat(line_outc,line_in,strlen(line_in));
  315.     strcat(line_outc,border);
  316.     strcat(line_outc,"├");
  317.     strncat(line_outc,hl,(72-strlen(line_in)-2)/2+odd);
  318.     strcat(line_outc,tr);
  319.     fputs(line_outc,outfilec);
  320.  
  321.     /**************************************
  322.      Read line of text until EOF and format
  323.      **************************************/
  324.         while(fgets(line_in,MAXLINE,infile)!=NULL){
  325.             strcpy(line_in,strtok(line_in,"\n"));
  326.             z=strlen(line_in);
  327.             odd=z-((z/2)*2);
  328.  
  329.             /********************************************
  330.              create centered plain text line with borders
  331.              ********************************************/
  332.             strcpy(line_out,lea);
  333.             strncat(line_out,hb,(72-strlen(line_in))/2);
  334.             strncat(line_out,line_in,strlen(line_in));
  335.             strncat(line_out,hb,(72-strlen(line_in))/2+odd);
  336.             strcat(line_out,rea);
  337.             fputs(line_out,outfile);
  338.  
  339.             /******************************************
  340.              create centered IBM char line with borders
  341.              ******************************************/
  342.             strcpy(line_outg,le);
  343.             strncat(line_outg,hb,(72-strlen(line_in))/2);
  344.             strncat(line_outg,line_in,strlen(line_in));
  345.             strncat(line_outg,hb,(72-strlen(line_in))/2+odd);
  346.             strcat(line_outg,re);
  347.             fputs(line_outg,outfileg);
  348.             printf("%s",line_outg);
  349.  
  350.             /*************************************************
  351.              create centered ansi color text line with borders
  352.              *************************************************/
  353.             strcpy(line_outc,border);
  354.             strcat(line_outc,le);
  355.             strcat(line_outc,text);
  356.             strncat(line_outc,hb,(72-strlen(line_in))/2);
  357.             strncat(line_outc,line_in,strlen(line_in));
  358.             strncat(line_outc,hb,(72-strlen(line_in))/2+odd);
  359.             strcat(line_outc,border);
  360.             strcat(line_outc,re);
  361.             fputs(line_outc,outfilec);
  362.             }
  363.  
  364.     /***********************************
  365.      make bottom border, plain & colored
  366.      ***********************************/
  367.     strcpy(line_out,bla);
  368.     strncat(line_out,hla,72);
  369.     strcat(line_out,bra);
  370.     fputs(line_out,outfile);
  371.     fputs("{PB\n",outfile);
  372.  
  373.     strcpy(line_outg,bl);
  374.     strncat(line_outg,hl,72);
  375.     strcat(line_outg,br);
  376.     fputs(line_outg,outfileg);
  377.     fputs("{PB\n",outfileg);
  378.  
  379.     fputs(line_outg,outfilec);
  380.     fputs(normal,outfilec);
  381.     fputs("{PB\n",outfilec);
  382.     printf("%s",line_outg);
  383.     fclose(infile);
  384.     }
  385. }
  386. /****************************
  387.  close main.nws and mainc.nws
  388.  all done.
  389.  ****************************/
  390.  
  391. fcloseall();
  392. printf("--> All done! Thank you for using NEWZ.");
  393. return(0);
  394. }
  395.  
  396. void no_news(void){
  397.  
  398. printf("    No applicable files found!\n");
  399. printf("--> Generating NO NEWS message for:\n");
  400. printf("    %s\n",main_path);
  401. printf("    %s\n",mainc_path);
  402. printf("    %s\n",maing_path);
  403. if((outfile=fopen(main_path,"wt"))==NULL){
  404.     printf("FAILURE TRYING TO OPEN MAIN.NWS!\n");
  405.     exit(1);
  406.     }
  407.  
  408. if((outfilec=fopen(mainc_path,"wt"))==NULL){
  409.     printf("FAILURE TRYING TO OPEN MAINC.NWS!\n");
  410.     exit(1);
  411.     }
  412.  
  413. if((outfileg=fopen(maing_path,"wt"))==NULL){
  414.     printf("FAILURE TRYING TO OPEN MAING.NWS!\n");
  415.     exit(1);
  416.     }
  417.  
  418. fputs(title,outfilec);
  419. fputs("No news items today.\n",outfile);
  420. fputs("No news items today.\n",outfilec);
  421. fputs("No news items today.\n",outfileg);
  422. fputs(normal,outfilec);
  423.  
  424. fcloseall();
  425. printf("--> All done, Thank You for using NEWZ!\n");
  426. exit(0);
  427. }
  428.  
  429.